<?php

class Student_AssessmentController extends Zend_Controller_Action
{
     public function init()
    {
        /* Initialize action controller here */
        
    }

   public function indexAction()
    {
        $this->view->title = "My Assessments";
       $this->view->headTitle($this->view->title, 'PREPEND');

       $assess = new Student_Model_DbTable_Assessment();
       $course_id = $this->_getParam('id');
       
        $this->view->id = $course_id;
      // echo $course_id;

      // $valid = $form->getValues();
      // print_r($valid);
      //echo $course_id;

       $allAssess = $assess->getSingleUserAssessments($course_id);
      // print_r($allAssess);
       $this->view->assessments = $allAssess;

    }

    public function addAction()
    {
        $this->view->title = "<b>Add Assessments</b><br>";
       $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Student_Form_newAssessment();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData))
                        {
    			$assess_name = $form->getValue('assess_name');
                        $assess_weight = $form->getValue('assess_weight');
                        $num_of_assign = $form->getValue('num_of_assign');
                        $course_id = $this->_getParam('id');
                        
    			$add = new Student_Model_DbTable_Assessment();
    			$add->addAssessment($assess_name, $assess_weight, $course_id, $num_of_assign);
    			$this->view->message = "$assess_name has been saved on your records.";
                        $this->_redirect('/student/assessment/index/id/'.$course_id);

                        }
                else
                    {
    			$form->populate($formData);
                    }
    }
    }

    
    public function editAction()
    {
        $this->view->title = "Edit Assessment Group";
        $this->view->headTitle($this->view->title);
        $form = new Student_Form_NewAssessment();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                        $id = $this->_getParam('id');
                        $assess_name = $form->getValue('assess_name');
                        $assess_weight = $form->getValue('assess_weight');
                        $num_of_assign = $form->getValue('num_of_assign');
    			$edit = new Student_Model_DbTable_Assessment();
                        $for_id = $edit->getAssessDetails($id);

                        $the_id=0;
                        foreach($for_id as $f_i){
                            $the_id = $f_i->id_of_course;
                        }

                       // $valid = $form->getValues();
                       // print_r($valid);
                        $edit->updateAssessment($id, $assess_name, $assess_weight, $num_of_assign);
                        $this->_redirect('/student/assessment/index/id/'.$the_id);
                }
                else
                {
                    $form->populate($formData);
                }
        }
        else
        {
            $id = $this->_getParam('id');
            if ($id > 0)
            {
                $assessments = new Student_Model_DbTable_Assessment();
                $form->populate($assessments->getAssessment($id));
            }
        }

    }

    public function viewAction()
    {

       $id = $this->_getParam('id');
       $assessments = new Student_Model_DbTable_Assessment();
       $assessment_details = $assessments->getAssessDetails($id);
       $this->view->assessments = $assessment_details;
       $this->_redirect('/student/assessment/index/id/'.$id);

       //$valid = $form->getValues();
      // print_r($valid);
       //echo $id;
       //$course_id = $this->_getParam('id', 0);
      // $courses = new Student_Model_DbTable_Course();

       
    }

   
    public function deleteAction()
    {
        $assess_id = $this->_getParam('id');
        $assessment = new Student_Model_DbTable_Assessment();
        $for_id = $assessment->getAssessDetails($assess_id);
        $the_id=0;
        foreach($for_id as $f_i){
                            $the_id = $f_i->id_of_course;
                        }

        $this->view->title = "Delete this group";
        $this->view->headTitle($this->view->title);
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $assessment = new Student_Model_DbTable_Assessment();
                echo $id;
                $assessment->deleteAssessment($id);
            }
            $this->_redirect('/student/assessment/index/id/'.$the_id);
            } else {

                //
             $id = $this->_getParam('id');
             $assess = new Student_Model_DbTable_Assessment();
             $delete = $assess->getAssessment($id);
                           //print_r($test);
              $this->view->assessment = $delete;
}
    }



}

